package cn.com.cifi.mars.service.impl;

import java.time.LocalDateTime;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import cn.com.cifi.mars.bean.vo.price.CoreWarnInfoVO;
import cn.com.cifi.mars.bean.vo.price.PriceApproveWarnInfoVO;
import cn.com.cifi.mars.entity.PriceApprovalCoreWarn;
import cn.com.cifi.mars.mapper.mars.PriceApprovalCoreWarnMapper;
import cn.com.cifi.mars.service.ApprovalPreviewService;
import cn.com.cifi.mars.service.PriceApprovalCoreWarnService;
import cn.com.cifi.mars.util.UUIDUtils;

/**
 * <p>
 * 货值审批核心指标表 服务实现类
 * </p>
 *
 * @author lyb
 * @since 2021-08-10
 */
@Service
public class PriceApprovalCoreWarnServiceImpl extends ServiceImpl<PriceApprovalCoreWarnMapper, PriceApprovalCoreWarn> implements PriceApprovalCoreWarnService {

	@Autowired
	private PriceApprovalCoreWarnMapper priceApprovalCoreWarnMapper;
	
	@Autowired
    private ApprovalPreviewService approvalPreviewService;
	
	
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void savePriceApprovalCoreWarnData(String id,String projectId, String versionState) {
		PriceApproveWarnInfoVO vo = approvalPreviewService.getWarnInfo("", id);
		if(vo != null) {
			List<CoreWarnInfoVO> coreWarnInfoList = vo.getCoreWarnInfoList();
			for(CoreWarnInfoVO obj : coreWarnInfoList) {
				PriceApprovalCoreWarn priceApprovalCoreWarn = new PriceApprovalCoreWarn();
				priceApprovalCoreWarn.setId(UUIDUtils.create());
				priceApprovalCoreWarn.setProjectId(projectId);
				priceApprovalCoreWarn.setBillId(id);
				priceApprovalCoreWarn.setState(versionState);
				priceApprovalCoreWarn.setObjName(obj.getName());
				priceApprovalCoreWarn.setProfitRate(obj.getProfit());
				priceApprovalCoreWarn.setInvestment(obj.getInvestment());
				priceApprovalCoreWarn.setStrategy(obj.getStrategy());
				priceApprovalCoreWarn.setPresident(obj.getPresident());
				priceApprovalCoreWarn.setBeforeData(obj.getBefore());
				priceApprovalCoreWarn.setAfterData(obj.getAfter());
				priceApprovalCoreWarn.setAfterInvestment(obj.getAfterInvestment());
				priceApprovalCoreWarn.setAfterStrategy(obj.getAfterStrategy());
				priceApprovalCoreWarn.setAfterPresident(obj.getAfterPresident());
				priceApprovalCoreWarn.setAfterBefore(obj.getAfterBfore());
				priceApprovalCoreWarn.setCreateTime(LocalDateTime.now());
				priceApprovalCoreWarnMapper.insert(priceApprovalCoreWarn);
			}
		}
		
	}

	@Override
	public List<PriceApprovalCoreWarn> selectByBusinessKey(String businessKey) {
		List<PriceApprovalCoreWarn> priceApprovalCoreWarns = priceApprovalCoreWarnMapper.selectList(new QueryWrapper<PriceApprovalCoreWarn>().eq("bill_id",businessKey));
		return priceApprovalCoreWarns;
	}

	@Override
	public boolean selectOne(String businessKey) {
		List<PriceApprovalCoreWarn> priceApprovalCoreWarnList = priceApprovalCoreWarnMapper.selectList(new QueryWrapper<PriceApprovalCoreWarn>().eq("bill_id",businessKey));
		if (priceApprovalCoreWarnList.isEmpty()){
			return false;
		}
		return true;
	}

}
